package com.outsider.product.controller;

import java.util.HashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.AccessToken;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.outsider.interceptor.ResponseHeaderInterceptor;
import com.outsider.product.dao.Product;
import com.outsider.product.dao.ProductRecord;
import com.outsider.product.service.ProductService;
import com.outsider.util.Const;
import com.outsider.util.JD;
import com.outsider.util.WatchDogeUtil;
import com.outsider.weixin.dao.User;


@Before(ResponseHeaderInterceptor.class)
public class ProductController extends Controller{
	private ProductService productService=new ProductService();
    public void addProduct(){
    	String name=getPara("name");
    	String openID=getPara(Const.OPEN_ID);
    	String JDURL=getPara("JDURL");
    	String DDURL=getPara("DDURL");
    	String AMURL=getPara("AMURL");
    	
    	Integer userID=User.dao.findIDbyOpenID(openID);
    	userID=1;
    	boolean flag=productService.addProduct(name,AMURL,DDURL,JDURL,userID);
    	if (flag)
    		renderJson(Const.RESULT, Const.SUCCESS);
    	else
    		renderJson(Const.RESULT, Const.FAIL);
    	
    }

    
    public void delete(){
    	int ID=getParaToInt();
    	if (Product.dao.deleteById(ID))
        	renderJson(Const.RESULT, Const.SUCCESS);
        else
        	renderJson(Const.RESULT, Const.FAIL);
    }
    public void getPage(){
    	String openID=getPara(Const.OPEN_ID);
    	Integer userID=User.dao.findIDbyOpenID(openID);
		int page=getParaToInt("page");
		int row=getParaToInt("row");
    	setAttr("page", productService.getPage(page, row, userID));
    	renderJson();
    }
    
    public void test(){
    	String code=getPara("code");
    	renderJson("code",code);
    }
    
    public void search(){
    	String name=getPara("name");
    	JSONObject result=WatchDogeUtil.search(name);
    	renderJson("result",result);
    }
    
    public void update(){
    	String openID=getPara(Const.OPEN_ID);
    	setAttr("flag", productService.updateRecord(openID));
    	renderJson();
    }

}
